<template>
  <div class="userleveBox">
    <!-- 搜索 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="等级名称">
        <el-input v-model="searchIt" placeholder="等级名称"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          v-model="searchIsShow"
          @change="searchIsShowFun"
          placeholder="状态"
        >
          <el-option label="显示" :value="1"></el-option>
          <el-option label="隐藏" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="btnBox">
      <el-button type="primary" @click="tjFun">添加按钮</el-button>
    </div>
    <el-table :data="tableData.list" border style="width: 100%">
      <el-table-column fixed prop="id" label="ID" width="140">
      </el-table-column>
      <!-- 图片懒加载+图片预览 -->
      <el-table-column prop="image" label="等级图标" width="140">
        <template class="imgBox" slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.image"
            :preview-src-list="[scope.row.image]"
          >
            <div slot="error" class="image-slot">
              <img src="/img/索大.gif" alt="" />
            </div>
            <div slot="placeholder" class="image-slot">
              <img src="/img/Default.jpg" alt="" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="等级名称" width="140">
      </el-table-column>
      <el-table-column prop="grade" label="等级" width="140"> </el-table-column>
      <el-table-column prop="discount" label="享受折扣" width="140">
      </el-table-column>
      <el-table-column prop="zip" label="是否显示" width="160">
        <template slot-scope="scope">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            @change="is_Show(scope.row)"
            v-model="scope.row.is_show"
            active-text="显示"
            inactive-text="隐藏"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="explain" label="等级说明" width="140">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template slot-scope="scope">
          <el-button @click="delFun(scope.row)" type="text" size="small"
            >删除</el-button
          >
          <el-button type="text" size="small" @click="updataShowFun(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      @current-change="currentChangeFun"
      :page-size="3"
      :current-page="page"
      layout="prev, pager, next"
      :total="tableData.count"
    >
    </el-pagination>
    <!-- 弹窗 -->
    <el-dialog :title="tit" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="等级名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="7">
            <el-form-item label="等级" :label-width="formLabelWidth">
              <el-input
                v-model="form.grade"
                type="number"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="享受折扣" :label-width="formLabelWidth">
              <el-input v-model="form.discount" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="等级经验值" :label-width="formLabelWidth">
              <el-input v-model="form.exp_num" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="图标" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :http-request="iconUploadFun"
          >
            <img v-if="form.icon" :src="form.icon" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="会员背景" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :http-request="imageUploadFun"
          >
            <img v-if="form.image" :src="form.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否显示" :label-width="formLabelWidth">
          <el-radio v-model="form.is_show" :label="1">显示</el-radio>
          <el-radio v-model="form.is_show" :label="0">隐藏</el-radio>
        </el-form-item>
        <el-form-item label="等级说明" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="form.explain"
          >
          </el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addFun">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  vipList,
  is_Show,
  upload,
  AdduserLevel,
  delectFun,
} from "@/api/request";
export default {
  data() {
    return {
      tit: "添加",
      dialogFormVisible: false,
      formLabelWidth: "90px",
      imageUrl: "",
      formInline: {
        user: "",
        region: "",
      },
      page: 1,
      limit: 3,
      tableData: [],
      form: {
        discount: "",
        exp_num: "",
        explain: "",
        grade: "",
        icon: "",
        image: "",
        is_show: "",
        name: "",
      },
      searchIsShow: "",
      searchIt: "",
    };
  },
  methods: {
    // 点击添加弹出窗
    tjFun() {
      this.tit = "添加";
      this.dialogFormVisible = true;
      Object.keys(this.form).map((key) => {
        this.form[key] = "";
      });
    },
    // 点击编辑添加弹出窗
    updataShowFun(val) {
      this.tit = "编辑";
      this.dialogFormVisible = true;
      this.form = { ...val };
    },
    // 删除
    delFun(val) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delectFun({ id: val.id }).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
              duration: 500,
            });
            this.getJson();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 搜索
    searchIsShowFun(val) {
      this.getJson();
    },
    onSubmit() {
      // console.log('111')
      console.log(this.searchIt);
      this.getJson();
    },
    // 图标上传
    iconUploadFun(file) {
      let fromdata = new FormData();
      fromdata.append("pid", 0);
      fromdata.append("file", file.file);
      upload(fromdata).then((res) => {
        this.form.icon = "http://47.93.101.203" + res.data.src;
        this.imageUrl = res.data.src;
      });
    },
    // 图片上传
    imageUploadFun(file) {
      let fromdata = new FormData();
      fromdata.append("pid", 0);
      fromdata.append("file", file.file);
      upload(fromdata).then((res) => {
        this.form.image = "http://47.93.101.203" + res.data.src;
      });
    },
    // 添加
    addFun() {
      AdduserLevel(this.form).then((res) => {
        this.$message({
          $message: res.msg,
          type: res.status == 200 ? "success" : "error",
          duration: 500,
        });
        if (res.status == 200) {
          this.getJson();
          this.dialogFormVisible = false;
        }
      });
    },

    // 获取数据
    getJson() {
      vipList({
        is_show: this.searchIsShow,
        title: this.searchIt,
        page: this.page,
        limit: this.limit,
      }).then((res) => {
        this.tableData = res.data;
        console.log(res);
      });
    },
    // 状态
    is_Show(val) {
      console.log(val);
      is_Show({ id: val.id, is_show: val.is_show }).then((res) => {
        this.getJson();
        this.$notify({
          title: res.status == 200 ? "成功" : "失败",
          message: res.msg,
          type: res.status == 200 ? "success" : "",
        });
      });
    },
    currentChangeFun(page) {
      this.page = page;
      this.getJson();
    },
  },
  // 挂载完成之后执行
  created() {
    this.getJson();
  },
};
</script>
<style lang="less" >
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
img {
  width: 60px !important;
  height: 60px !important;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
button {
  width: 100px;
  height: 40px;
}
</style>
